package com.example.autopia.view.Adapters

import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.animation.AnimationUtils
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.autopia.R
import com.example.autopia.databinding.ViewholderCommentItemBinding
import com.example.autopia.model.Item.Comment
import com.example.autopia.utilities.ImageManager
import com.example.autopia.utilities.TimeManager
import com.example.autopia.view.Listeners.ChannelsListener
import com.example.autopia.view.Listeners.CommentListener
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase

class CommentsAdapter   :
    RecyclerView.Adapter<CommentsAdapter.ViewHolder>() {

    private lateinit var list : MutableList<Comment>
    private lateinit var listenerComment: CommentListener
    private lateinit var listenerChannel: ChannelsListener

    fun attachListener(listener: CommentListener) {
        this.listenerComment = listener
    }
    fun attachListenerChannels(listener: ChannelsListener) {
        this.listenerChannel = listener
    }
    fun setData(newList : List<Comment>){
        list = newList.toMutableList()
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflater = LayoutInflater.from(parent.context)

        val binding = ViewholderCommentItemBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return ViewHolder(binding,inflater)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val news = list[position]
        holder.bind(news,listenerComment,listenerChannel)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    inner class ViewHolder(private val binding: ViewholderCommentItemBinding,private val inflater: LayoutInflater) :
        RecyclerView.ViewHolder(binding.root) {
        private val imageManager = ImageManager()
        private val timeManager = TimeManager()
        fun bind(comment: Comment, listenerComment: CommentListener, listenerChannel: ChannelsListener) {
            binding.apply {
                tvName.text = comment.username
                val firebaseAuth = Firebase.auth

                Glide.with(itemView)
                    .load(imageManager.byteArrayToBitmap(comment.userphoto))
                    .centerCrop()
                    .placeholder(R.drawable.logo_placeholder)
                    .error(R.drawable.logo_placeholder)
                    .into(ivLogo)

                tvTime.text = timeManager.unixTimeToDateTimeComment(comment.time)
                tvMainText.text = comment.text

                val popupView = inflater.inflate(R.layout.news_popup_layout, null)
                val popupWindow = PopupWindow(
                    popupView,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    true
                )

                val enterAnimation = AnimationUtils.loadAnimation(inflater.context, R.anim.popup_enter_animation)
                popupView.startAnimation(enterAnimation)
                val delete = popupView.findViewById<LinearLayout>(R.id.delete)
                val text = popupView.findViewById<TextView>(R.id.text)
                text.text = "Удалить"
                delete.setOnClickListener {
                        listenerComment.onCommentClick(comment = comment)
                        popupWindow.dismiss()

                }
                val animation = AnimationUtils.loadAnimation(inflater.context, R.anim.fade_in)
                binding.root.startAnimation(animation)

                ivLogo.setOnClickListener {
                    listenerChannel.onChannelClick( userid = comment.userid)
                }
                Body.setOnLongClickListener {
                    if (comment.userid == (firebaseAuth.currentUser?.uid ?: "")){
                        popupWindow.showAsDropDown(binding.Body)
                    }
                    true
                }
            }
        }
    }
}